﻿@{
    Layout = "~/Views/Shared/_FormWhite.cshtml";
}

<div class="wrapper animated fadeInRight">
    <form id="form" class="form-horizontal m">
        <div class="form-group">
            <label class="col-sm-3 control-label ">上级部门</label>
            <div class="col-sm-8">
                <div id="parentId" col="ParentId"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">部门名称<font class="red"> *</font></label>
            <div class="col-sm-8">
                <input id="departmentName" col="DepartmentName" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">负责人</label>
            <div class="col-sm-8">
                <div id="principalId" col="PrincipalId"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">电话号码</label>
            <div class="col-sm-8">
                <input id="telephone" col="Telephone" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">传真</label>
            <div class="col-sm-8">
                <input id="fax" col="Fax" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">Email</label>
            <div class="col-sm-8">
                <input id="email" col="Email" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">显示顺序</label>
            <div class="col-sm-8">
                <input id="departmentSort" col="DepartmentSort" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">备注</label>
            <div class="col-sm-8">
                <textarea id="remark" col="Remark" class="form-control"></textarea>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    var id = ys.request("id");
    $(function () {
        $('#parentId').ysComboBoxTree({ url: '@Url.Content("~/OrganizationManage/Department/GetDepartmentTreeListJson")', async: false });
        $('#principalId').ysComboBoxTree({ url: '@Url.Content("~/OrganizationManage/Department/GetUserTreeListJson")', async: false });

        getForm();

        $("#form").validate({
            rules: {
                departmentName: { required: true },
                parentIdInput: { required: true }
            }
        });
    });

    function getForm() {
        if (id > 0) {
            ys.ajax({
                url: '@Url.Content("~/OrganizationManage/Department/GetFormJson")' + '?id=' + id,
                type: "get",
                success: function (obj) {
                    if (obj.Tag == 1) {
                        var result = obj.Data;
                        $("#form").setWebControls(result);
                    }
                }
            });
        }
        else {
             ys.ajax({
                 url: '@Url.Content("~/OrganizationManage/Department/GetMaxSortJson")',
                type: "get",
                success: function (obj) {
                    if (obj.Tag == 1) {
                        $("#departmentSort").val(obj.Data);
                    }
                }
            });
        }
    }

    function saveForm(index) {
        if ($("#form").validate().form()) {
            var postData = $("#form").getWebControls({ Id: id });
            postData.ParentId = ys.getLastValue(postData.ParentId);
            postData.PrincipalId = ys.getLastValue(postData.PrincipalId);
            ys.ajax({
                url: '@Url.Content("~/OrganizationManage/Department/SaveFormJson")',
                type: "post",
                data: postData,
                success: function (obj) {
                    if (obj.Tag == 1) {
                        ys.msgSuccess(obj.Message);
                        parent.searchTreeGrid(obj.Data);
                        parent.layer.close(index);
                    }
                    else {
                        ys.msgError(obj.Message);
                    }
                }
            });
        }
    }
</script>
